package com.heima.wemedia.controller.v1;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.common.exception.CustomException;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmNewsDownOrUpDto;
import com.heima.model.wemedia.dtos.WmNewsDto;
import com.heima.model.wemedia.dtos.WmNewsPageReqDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.service.WmChannelService;
import com.heima.wemedia.service.WmNewsService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/news")
public class WmNewsController {

    @Autowired
    private WmNewsService wmNewsService;

    /**
     * 文章列表查询
     * @param dto
     * @return
     */
    @PostMapping("/list")
    public ResponseResult list(@RequestBody WmNewsPageReqDto dto){
        dto.checkParam();
//        dto.setBeginPubDate(dto.getBeginPubDate() + " 00:00:00");
//        dto.setEndPubDate(dto.getEndPubDate() + " 23:59:59");
        return wmNewsService.findPage(dto);
    }

    @PostMapping("/submit")
    public ResponseResult submit(@RequestBody WmNewsDto dto){
        //参数校验  解决参数校验通用性问题
        if(StringUtils.isBlank(dto.getTitle()) || dto.getTitle().length() <5 ||dto.getTitle().length() >32){
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID,"标题参数长度不正确");
        }
        wmNewsService.submit(dto);
        return ResponseResult.okResult(null);
    }

    /**
     * 文章上下架
     * @param dto
     * @return
     */
    @PostMapping("/down_or_up")
    public ResponseResult downOrUp(@RequestBody WmNewsDownOrUpDto dto){
        wmNewsService.downOrUp(dto);
        return ResponseResult.okResult(null);
    }
}
